using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace HarrisDesigns.Controls.dasBlogControls.Google.Reader
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class FeedItem
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("categories")]
        public string[] Categories { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("published")]
        public int PublishedSeconds { get; set; }

        [JsonIgnore]
        public DateTime Published
        {
            get
            {
                return (new DateTime(1970, 1, 1)).AddSeconds(PublishedSeconds);
            }
        }

        [JsonProperty("updated")]
        public int UpdatedSeconds { get; set; }

        [JsonIgnore]
        public DateTime Updated
        {
            get
            {
                return (new DateTime(1970, 1, 1)).AddSeconds(UpdatedSeconds);
            }
        }

        [JsonProperty("content")]
        public string Content { get; set; }

        [JsonProperty("summary")]
        public string Summary { get; set; }

        [JsonProperty("author")]
        public string Author { get; set; }

        [JsonProperty("alternate")]
        public FeedItemLink Alternate { get; set; }

        [JsonProperty("related")]
        public FeedItemLink Related { get; set; }

        [JsonProperty("origin")]
        public FeedItemOrigin Origin { get; set; }

        [JsonProperty("likingUsers")]
        public List<FeedItemLikingUser> LikingUsers { get; set; }

        [JsonProperty("comments")]
        public string[] Comments { get; set; }

        [JsonProperty("annotations")]
        public List<FeedItemAnnotations> Annotations { get; set; }

        public string GetTextAsHtml()
        {
            string html = Content;
            return ConvertUrlsToLinks(html);
        }


        public string ConvertUrlsToLinks(string html)
        {
            var linkReplace = new Regex(@"((https?|s?ftp|ssh)\:\/\/[^""\s\<\>]*[^.,;'"">\:\s\<\>\)\]\!])",
                                        RegexOptions.IgnoreCase);
            return linkReplace.Replace(html, @"<a href=""$1"">$1</a>");
        }
    }
}